//
//  ZLExamRecordViewController.m
//  ZLExamination
//
//  Created by Zheng on 2017/8/10.
//  Copyright © 2017年 Zheng. All rights reserved.
//

#import "ZLExamRecordViewController.h"
#import "ZLExamResultViewController.h"
#import "ZLExamBeginViewController.h"

#import "ZLExamRecordCell.h"
#import "ZLExamRecordModel.h"
#import "ZLBaseNavigationController.h"

static NSString *kZLExamRecordCell_ID = @"kZLExamRecordCell_ID";

@interface ZLExamRecordViewController ()

@property (nonatomic, strong) UIView *titleView;
@property (nonatomic, strong) UIButton *selectedBtn;
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation ZLExamRecordViewController

#pragma mark - 点击事件
- (void)titleClick:(UIButton *)sender {
    
    if (_selectedBtn == sender) {
        return;
    }
    
    _selectedBtn.selected = !_selectedBtn.selected;
    _selectedBtn = sender;
    _selectedBtn.selected = !_selectedBtn.selected;
    
    if ([sender.titleLabel.text isEqualToString:@"全部"]) {
        [self loadDataWithType:nil];
    } else if ([sender.titleLabel.text isEqualToString:@"当月"]) {
        [self loadDataWithType:@"1"];
    } else {
        [self loadDataWithType:@"2"];
    }

}


#pragma mark - 视图生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"考试记录";
    
    [self.myTableView registerClass:[ZLExamRecordCell class] forCellReuseIdentifier:kZLExamRecordCell_ID];
    
//    self.navigationItem.titleView = self.titleView;
    
//    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self loadDataWithType:nil];
}

#pragma mark - 加载数据
- (void)loadDataWithType:(NSString *)type {
    
    NSDictionary *param;
    if (type) {
        param = @{@"type":type};
    }
    
    _dataArr = [NSMutableArray new];
    
    [[YCRequestDataService sharedManager] Post:@"paper/paper/paper_list" parameters:param success:^(id response) {
        if (response[@"error_code"]) {
            NSLog(@"%@",response[@"error_msg"]);
        } else {
            NSArray *lists = response[@"response_data"][@"lists"];
            [lists enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                ZLExamRecordModel *model = [[ZLExamRecordModel alloc] initWithDataDic:obj];
                [_dataArr addObject:model];
            }];
            
            [self.myTableView reloadData];
        }
    } failure:^{
        NSLog(@"网络或服务器出错");
    }];
    
}

#pragma mark - 子视图
- (UIView *)titleView {
    if (!_titleView) {
        _titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 189, 44)];
        _titleView.center = CGPointMake(self.view.center.x, 0);
        
        UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn1.frame = CGRectMake(0, 0, 35, 44);
        [btn1 setTitle:@"全部" forState:UIControlStateNormal];
        [btn1 setTitleColor:[UIColor colorWithWhite:0.9 alpha:1] forState:UIControlStateNormal];
        [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        btn1.titleLabel.font = kMyFont(14);
        [btn1 addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        btn1.selected = YES;
        [_titleView addSubview:btn1];
        
        _selectedBtn = btn1;
        
        UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn2.frame = CGRectMake(90-17.5, 0, 35, 44);
        [btn2 setTitle:@"当月" forState:UIControlStateNormal];
        [btn2 setTitleColor:[UIColor colorWithWhite:0.9 alpha:1] forState:UIControlStateNormal];
        [btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        btn2.titleLabel.font = kMyFont(14);
        [btn2 addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [_titleView addSubview:btn2];

        UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn3.frame = CGRectMake(189-44, 0, 44, 44);
        [btn3 setTitle:@"未完成" forState:UIControlStateNormal];
        [btn3 setTitleColor:[UIColor colorWithWhite:0.9 alpha:1] forState:UIControlStateNormal];
        [btn3 setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        btn3.titleLabel.font = kMyFont(14);
        [btn3 addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [_titleView addSubview:btn3];
        
        UIView *lineView1 = [[UIView alloc] init];
        lineView1.frame = (CGRect){55,14.5,1,15};
        lineView1.backgroundColor = [UIColor whiteColor];
        [_titleView addSubview:lineView1];
        
        UIView *lineView2 = [[UIView alloc] init];
        lineView2.frame = (CGRect){125,14.5,1,15};
        lineView2.backgroundColor = [UIColor whiteColor];
        [_titleView addSubview:lineView2];
        
    }
    return _titleView;
}


#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZLExamRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:kZLExamRecordCell_ID forIndexPath:indexPath];
    
    if (_dataArr.count != 0) {
        cell.model = _dataArr[indexPath.section];
    }
    
    return cell;
}

#pragma mark - tableview delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 7;
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    ZLExamRecordCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//
//    switch ([cell getExamState]) {
//        case ZLExamRecordCellStylePass: {
//            //跳转考试结果 合格
//            ZLExamResultViewController *erVC = [[ZLExamResultViewController alloc] init];
//            ZLExamRecordModel *model = _dataArr[indexPath.section];
//            erVC.user_paper_id = model.x_id;
//            ZLBaseNavigationController *nav = [[ZLBaseNavigationController alloc] initWithRootViewController:erVC];
//
//            kDISPATCH_MAIN_THREAD(^{
//                [self presentViewController:nav animated:YES completion:nil];
//            })
//
//        }
//            break;
//        case ZLExamRecordCellStyleUnFinish: {
//            //跳转继续考试
//            //TODO:
//            ZLExamBeginViewController *examVC = [[ZLExamBeginViewController alloc] init];
//            ZLExamRecordModel *model = _dataArr[indexPath.section];
//            examVC.curriculum_id = model.curriculum_id;
//            ZLBaseNavigationController *nav = [[ZLBaseNavigationController alloc] initWithRootViewController:examVC];
//            examVC.actionStyle = ActionStyleContinue;
//
//            kDISPATCH_MAIN_THREAD(^{
//                [self presentViewController:nav animated:YES completion:nil];
//            })
//        }
//            break;
//        case ZLExamRecordCellStyleUnPass: {
//            //跳转考试结果 不合格
//            ZLExamResultViewController *erVC = [[ZLExamResultViewController alloc] init];
//            ZLExamRecordModel *model = _dataArr[indexPath.section];
//            erVC.user_paper_id = model.x_id;
//            erVC.curriculum_id = model.curriculum_id;
//            ZLBaseNavigationController *nav = [[ZLBaseNavigationController alloc] initWithRootViewController:erVC];
//
//            kDISPATCH_MAIN_THREAD(^{
//                [self presentViewController:nav animated:YES completion:nil];
//            })
//
//        }
//            break;
//    }
//}


#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
